package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.listener.DataListener1;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;


@RestController
@RequestMapping("/sport")
public class SportController {
    @Reference
   private SportService sportService;
//    文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file ){
        DataListener1 dataListener1 = new DataListener1();
        try {
            EasyExcel.read(file.getInputStream(), Sport.class,dataListener1).sheet().doRead();
            List<Sport> list = dataListener1.getList();
            sportService.upload(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    //增加
    @RequestMapping("/addSport")
    public Result add(@RequestBody Sport sport) {
        try {
            sportService.add(sport);
            System.out.println(sport);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    //删除
    @RequestMapping("/deleteSport")//CHECKITEM_DELETE
    public Result deleteById(Integer id) {
        try {
            sportService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //编辑
    @RequestMapping("/editSport")
    public Result edit(@RequestBody Sport sport) {
        try {
            sportService.update(sport);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //查询
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult;
        try {
            pageResult = sportService.findPage(queryPageBean);
        } catch (Exception e) {
            return new Result(false, "查询失败");
        }
        return new Result(true, "查询成功", pageResult);
    }

    /*//模糊查询
    @RequestMapping("/findBySportName")
    public Result findBySportName(String sportname){
        try {
            Sport bySportName = sportService.findBySportName(sportname);
            return new Result(true,MessageConstant.QUERY_User_SUCCESS,bySportName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_User_FAIL);
        }*/

}
